package com.qf.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.product.entity.Tenant;
import com.qf.user.entity.Role;
import com.qf.user.entity.RoleMenu;
import com.qf.user.entity.User;
import com.qf.user.mapper.RoleMapper;
import com.qf.user.mapper.RoleMenuMapper;
import com.qf.user.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 角色管理 服务实现类
 * </p>
 *
 * @author yxh
 * @since 2023-10-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public PageResult<Role> findPage(PageDto<Role> pageDto) {
        IPage<Role> page = new Page<>(pageDto.getNow(),pageDto.getSize());
        Role where = pageDto.getWhere();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        //根据名称查询
        if(!StringUtils.isEmpty(where.getName())){
            queryWrapper.lambda().like(Role::getName,where.getName());
        }
        IPage<Role> PageInfo = this.baseMapper.selectPage(page, queryWrapper);
        PageResult<Role> pageResult = new PageResult<>();
        pageResult.setPages(PageInfo.getPages());
        pageResult.setList(PageInfo.getRecords());
        pageResult.setTotal(PageInfo.getTotal());
        return pageResult;

    }

    @Override
    public R add(Role role) {

        role.setCreateTime(new Date());
        int i = this.baseMapper.insert(role);
        for (String menuId : role.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menuId);
            roleMenu.setCreateTime(new Date());
            roleMenuMapper.insert(roleMenu);
        }
        if (i>0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    @Override
    public R update(Role role) {

        //角色权限修改暂时处理(先删除角色的所有权限，再重新加权限）
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleMenu::getRoleId, role.getId());
        roleMenuMapper.delete(queryWrapper);
        for (String menuId : role.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menuId);
            roleMenu.setCreateTime(new Date());
            roleMenuMapper.insert(roleMenu);
        }
        int i = this.baseMapper.updateById(role);
        if (i>0){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
}
